//
//  CHButton.swift
//  CHSimpleUI
//
//  Created by cyptt on 2022/7/15.
//

import UIKit

/*
 图片的位置
 */
public enum BUTTON_IMAGE_LOCAL_TYPE : Int {
    
    case left = 0    //左边
    case right = 1  //右
    case top = 2   //上
    case bottom = 3  //下
}
public  extension UIButton{
    
    /*
     图片位置上下左右
     @param local 位置
     @param margin 图片和字体间距
     */
    func imageLocal(local:BUTTON_IMAGE_LOCAL_TYPE,margin:CGFloat){
        
      
        let imageW:CGFloat = self.currentImage?.size.width ?? 0
        let imageH:CGFloat = self.currentImage?.size.height ?? 0
        
        let titleW:CGFloat = self.titleLabel?.intrinsicContentSize.width ?? 0
        let titleH:CGFloat = self.titleLabel?.intrinsicContentSize.height ?? 0
        
        if local == .left{
           
           self.imageEdgeInsets =  UIEdgeInsets.init(top: 0, left: -margin/2, bottom: 0, right: margin/2)
           
           self.titleEdgeInsets =  UIEdgeInsets.init(top: 0, left: margin/2, bottom: 0, right:-margin/2)
           
         
       }else if local == .right {
            
            self.imageEdgeInsets =  UIEdgeInsets.init(top: 0, left: titleW + margin/2, bottom: 0, right: -titleW - margin/2)
            
            self.titleEdgeInsets =  UIEdgeInsets.init(top: 0, left: -imageW - margin/2, bottom: 0, right:imageW + margin/2)
         
        } else if local == .top{
            
//            self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: (imageW + titleW)/2, bottom: (titleH + imageH)/2 + margin/2, right: -(imageW + titleW)/2)
            self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left:0 , bottom: (imageH - titleH)/2 + margin, right:-titleW)
            self.titleEdgeInsets = UIEdgeInsets.init(top:(imageH/2 + titleH) + margin , left:-imageW, bottom: 0, right:0)
          
          
//            self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: (imageW + titleW)/2, bottom: (titleH + imageH)/2 + margin/2, right: 0)
//
//            self.titleEdgeInsets = UIEdgeInsets.init(top: (titleH + imageH)/2 +  margin/2, left:0, bottom: 0, right:imageW/2)
//
        } else if local == .bottom{
            self.imageEdgeInsets = UIEdgeInsets.init(top: (imageH - titleH)/2 + margin, left:0, bottom: 0, right:-titleW)
            self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -imageW, bottom: (imageH/2 + titleH) + margin, right: 0)
           
            
            
//            self.imageEdgeInsets = UIEdgeInsets.init(top: (imageH - titleH)/2 + margin/2, left:0, bottom: 0, right:-titleW)
//            self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -imageW, bottom: (titleH + imageH)/2 + margin/2, right: 0)
        }
       
    }
}
